package com.javathread.th5.bitwise;

public class IntToBinary {
	
	public static void main(String[] args) {
		int data = 1548455;
		System.out.println("the data is " + Integer.toBinaryString(data));
		//位与 & (1&1=1 1&0=0 0&0=0)
		System.out.println("the 4 is " + Integer.toBinaryString(4));
		System.out.println("the 6 is " + Integer.toBinaryString(6));
		System.out.println("the 10&5 is " + Integer.toBinaryString(10&5)+ " 10进制等于" + (10&5));
		//位或| (1|1=1 1|0=1 0|0=0)
		System.out.println("the 5|10 is " + Integer.toBinaryString(4|6)+ " 10进制等于" + (5|10));
		//位非~ (~1=0 ~0=1)
		System.out.println("the ~4 is " + Integer.toBinaryString(~4)+ " 10进制等于" + (~4));
		System.out.println("the ~6 is " + Integer.toBinaryString(~6)+ " 10进制等于" + (~6));
		//位异或^ (1^1=0 1^0=1 0^0=0)
		System.out.println("the 1^1 is " + Integer.toBinaryString(1^1));
		System.out.println("the 1^0 is " + Integer.toBinaryString(1^0));
		System.out.println("the 0^0 is " + Integer.toBinaryString(0^0));
		
		// << 有符号左移     >>有符号右移     >>>无符号右移
		int left = 8;
		int result = left<<2;
		int result2 = left>>2;
		int result3 = left>>>2;
		System.out.println("the result is " + Integer.toBinaryString(result) + " 10进制等于" + (result));
		System.out.println("the result2 is " + Integer.toBinaryString(result2) + " 10进制等于" + (result2));
		System.out.println("the result3 is " + Integer.toBinaryString(result3) + " 10进制等于" + (result3));
		// 取模的操作 a % (2^n)  等价于  a & (2^n -1)
		System.out.println("the 345 % 16 is " + (345 % 16) + " or " + (345 & (16-1)));
		System.out.println("the 345 % 8 is " + (345 % 8) + " or " + (345 & (8-1)));
		
		int l1 = 4;
		int l2 = 29;
		System.out.println("the "+l1+"&"+l2+" is  等于" + (l1&l2));
		System.out.println("the "+l1+"|"+l2+" is 等于" + (l1|l2));
	}
}
